//
// Created by 成飞 on 2024/1/4.
//

#ifndef ORDERKV_LEVELDB_H
#define ORDERKV_LEVELDB_H



#include "leveldb/db.h"
#include "lsm_tree.h"
#include <atomic>

namespace lsm {

    class LevelDB : public LsmTree {

    private:
        leveldb::DB *db{};
        std::string lsmPath;

        std::atomic<leveldb::ReadOptions*> pReadOption{};
        leveldb::ReadOptions defaultReadOption{};
        leveldb::ReadOptions snapshotReadOption{};

    public:
        explicit LevelDB(std::string &lsmPath) ;

        bool put(std::string_view key, std::string_view val) override;

        bool batch_put(const std::vector<std::pair<std::string_view, std::string_view>> &kvPairBatch) override;

        bool query(std::string_view key, std::string& ret) override;

        bool range_query(const string &startKey, const string &endKey, int len, vector<pair<string , lsm::LsmRecordVal>>& valLocations, array<lsm::ScanInfo*, FILE_NO_MAX>& scanIndex) override;

        bool range_query2(const std::string &startKey, const std::string &endKey, int len, std::vector<std::pair<std::string , std::string>>& ret) override;


        void snapshot() override;

        void release_snapshot() override;
    private:
        void init_db();
    };

}


#endif //ORDERKV_LEVELDB_H
